<template>
    <div class="access-container">
        <div class="access-text">投融资类别赋权</div>
        <el-checkbox-group v-model="formList" :style="{'marginBottom': '50px'}">
            <el-checkbox v-for="(item,index) in formSelectList" :label="item.name" :key="item.id"/>
        </el-checkbox-group>
        <div class="access-text">行业类赋权</div>
        <el-checkbox-group v-model="businessList" :style="{'marginBottom': '50px'}">
            <el-checkbox v-for="(item, index) in businessSelectList" :label="item.name" :key="item.id"/>
        </el-checkbox-group>
        <div class="access-text">信息池赋权</div>
        <el-checkbox-group v-model="poolList" :style="{'marginBottom': '50px'}">
            <el-checkbox v-for="(item, index) in poolSelectList" :label="item.name" :key="item.id"/>
        </el-checkbox-group>
        <el-button type="success" @click="handleAccess">
            确认赋权
        </el-button>
    </div>
</template>

<script>
    import {getMetaClassList} from "../../api/meta-class";
    import {accessEnterprise, getOneEnterprise} from "../../api/enterprise";
    import {accessBank, getOneBank} from "../../api/bank";

    export default {
        name: 'access',
        data() {
            return {
                formList: [],
                businessList: [],
                poolList: [],
                formSelectList: [],
                businessSelectList: [],
                poolSelectList: [],
                role: '',
                uid: ''
            }
        },
        methods: {
            initList(form, business, pool) {
                if(!form) {
                    form = []
                }
                if(!business) {
                    business = []
                }
                if(!pool) {
                    pool = []
                }
                this.formList = form
                this.businessList = business
                this.poolList = pool
                console.log(this.formList)
                console.log(this.businessList)
                console.log(this.poolList)
            },
            handleAccess() {
                console.log(this.formList)
                console.log(this.businessList)
                console.log(this.poolList)
                const param = {
                    'id': this.uid,
                    'form': JSON.stringify(this.formList),
                    'business': JSON.stringify(this.businessList),
                    'pool': JSON.stringify(this.poolList)
                }
                if(this.role === 'enterprise') {
                    accessEnterprise(param).then(res => {
                        window.location.reload()
                    })
                } else {
                    accessBank(param).then(res => {
                        window.location.reload()
                    })
                }
            }
        },
        mounted() {
            // console.log(this.$route.query.id)
            // console.log(this.$route.query.role)
            this.uid = this.$route.query.id
            this.role = this.$route.query.role
            if(this.role === 'enterprise') {
                let param = {id: this.uid}
                getOneEnterprise(param).then(res => {
                    let form = JSON.parse(res.data.formAccess)
                    let business = JSON.parse(res.data.businessAccess)
                    let pool = JSON.parse(res.data.poolAccess)
                    this.initList(form, business, pool)
                })
            } else {
                let param = {id: this.uid}
                getOneBank(param).then(res => {
                    let form = JSON.parse(res.data.formAccess)
                    let business = JSON.parse(res.data.businessAccess)
                    let pool = JSON.parse(res.data.poolAccess)
                    this.initList(form, business, pool)
                })
            }
            getMetaClassList({type: 'all'}).then(res => {
                let list = res.data
                let form = []
                let business = []
                let pool = []
                for(let i = 0; i < list.length; ++i) {
                    if (list[i].type === 'form') {
                        form.push(list[i])
                    } else if(list[i].type === 'business') {
                        business.push(list[i])
                    } else {
                        pool.push(list[i])
                    }
                }
                this.formSelectList = form
                this.businessSelectList = business
                this.poolSelectList = pool
            })
        }
    }
</script>

<style lang="scss" scoped>
    .access {
        &-container {
            margin: 30px;
        }
        &-text {
            font-size: 30px;
            line-height: 46px;
            margin-bottom: 20px;
        }
    }
</style>

